{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "词典中的最大词长:  5\n",
      "正向最大匹配法FMM, 分词结果: ['南京市长', '江', '大桥', '非常宏伟']\n",
      "逆向最大匹配法RMM, 分词结果:  ['南京市', '长江大桥', '非常宏伟']\n",
      "双向最大匹配法BMM,分词结果:  ['南京市', '长江大桥', '非常宏伟']\n"
     ]
    }
   ],
   "source": [
    "import string, re\n",
    "def load_dict(filename):\n",
    "    f = open(filename, 'r',encoding = 'utf8').read()\n",
    "    maxLen = 1\n",
    "    dict = f.split(\"\\n\")\n",
    "    for i in dict:\n",
    "        if len(i)>maxLen:\n",
    "            maxLen = len(i)\n",
    "    return dict, maxLen;\n",
    "def FMM(dict, maxLen, text):\n",
    "    cut_word = []\n",
    "    start = 0\n",
    "    textLen = len(text)\n",
    "    while start != textLen:\n",
    "        index = start + maxLen\n",
    "        if index > len(text):\n",
    "            index = len(text)\n",
    "        for i in range(maxLen):\n",
    "            if(text[start:index] in dict) or (len(text[start:index]) == 1):\n",
    "                cut_word.append(text[start:index])\n",
    "                start = index\n",
    "                break\n",
    "            index -= 1\n",
    "    return cut_word\n",
    "def RMM(dict, maxLen, text):\n",
    "    cut_word = []\n",
    "    textLen = len(text)\n",
    "    while textLen > 0:\n",
    "        j = 0\n",
    "        for size in range(maxLen, 0, -1):\n",
    "            if textLen < size:\n",
    "                continue\n",
    "            cutword = text[textLen-size:textLen]\n",
    "            if cutword in dict:\n",
    "                cut_word.append(cutword)\n",
    "                textLen -= size\n",
    "                j +=1\n",
    "                break\n",
    "        if j == 0:\n",
    "            textLen -= 1\n",
    "    cut_word = list(reversed(cut_word))\n",
    "    return cut_word\n",
    "def BMM(dict, maxLen, text):\n",
    "        fmm = FMM(dict, maxLen, text)\n",
    "        rmm = RMM(dict, maxLen, text)\n",
    "        if len(fmm) != len(rmm):\n",
    "            if len(fmm) < len(rmm):\n",
    "                return fmm\n",
    "            else:\n",
    "                return rmm\n",
    "        else:\n",
    "            if fmm == rum:\n",
    "                return fmm\n",
    "            else:\n",
    "                fmm_1num = len([i for i in fmm if len(i) == 1])\n",
    "                rmm_1num = len([i for i in rmm if len(i) == 1])\n",
    "                return fmm_1num if fmm_1num < rmm_1num else rmm_1num\n",
    "def main():\n",
    "    dict, maxLen = load_dict('dict1.txt')\n",
    "    print(\"词典中的最大词长: \", maxLen)\n",
    "    text = \"南京市长江大桥非常宏伟\"\n",
    "    fmm_cut = FMM(dict, maxLen, text)\n",
    "    print(\"正向最大匹配法FMM, 分词结果:\", fmm_cut)\n",
    "    rmm_cut = RMM(dict, maxLen, text)\n",
    "    print(\"逆向最大匹配法RMM, 分词结果: \",rmm_cut)\n",
    "    bmm_cut = BMM(dict, maxLen, text)\n",
    "    print(\"双向最大匹配法BMM,分词结果: \", bmm_cut)\n",
    "if __name__=='__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
